package com.dreamers.extend;

import com.dreamers.EquipFrame.Time;
import com.dreamers.core.Collider2D;
import com.dreamers.core.MonoBehaviour;
import com.dreamers.map.MapArray;
import com.dreamers.pathcore.AStarTransform;
import com.dreamers.pathcore.Vector2;
import com.dreamers.pathcore.AStarPriorityQueue;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.List;

public class GameManager extends MonoBehaviour {

    Vector2 startPoint;
    Vector2 endPoint;

    @Override
    public void render(Graphics graphics) {

    }

    @Override
    public void mouseClicked(MouseEvent e) {


        /**
         * 鼠标的坐标跟矩形的坐标是一样的
         */
        long lastTime = Time.getCurrentTimeMillis();
        Collider2D[][] collider2DS = AStarWorld.collider2DS;
        int[][] map = MapArray.map;
        for (int y = 0; y < collider2DS.length; y++) {
            for (int x = 0; x < collider2DS[0].length; x++) {
                if (collider2DS[y][x].getRectangle().contains(e.getPoint())) {
                    if (startPoint == null) {
                        startPoint = new Vector2(x, y);
                        map[y][x] = 5;
                    } else if (endPoint == null) {
                        endPoint = new Vector2(x, y);
                        map[y][x] = 5;
                    }
                    if (startPoint != null && endPoint != null) {
                        AStarPriorityQueue aStar = new AStarPriorityQueue();
                        aStar.setStartPoint(startPoint.getX(), startPoint.getY());
                        aStar.setEndPoint(endPoint.getX(), endPoint.getY());
                        List<AStarTransform> aStarTransformList = aStar.AStarRun();

                        if (aStarTransformList != null) {
                            for (int i = 0; i < aStarTransformList.size(); ) {
                                if (Time.getCurrentTimeMillis()-lastTime>1000){
                                    AStarTransform aStarTransform = aStarTransformList.get(i);
                                    map[aStarTransform.getPosition().getY()][aStarTransform.getPosition().getX()] = 3;
                                    lastTime=Time.getCurrentTimeMillis();
                                    i++;
                                }
                            }

                           /* for (AStarTransform aStarTransform : aStarTransformList) {
                                map[aStarTransform.getPosition().getY()][aStarTransform.getPosition().getX()] = 3;
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException interruptedException) {
                                    interruptedException.printStackTrace();
                                }
                            }*/
                        }

                        startPoint = null;
                        endPoint = null;
                    }
                }
            }
        }
    }
}
